import { createRouter, createWebHistory } from "vue-router";

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/LoginView.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/Home/HomeView.vue')
  },
]

const router = createRouter( {
  history: createWebHistory(),
  routes
} )

router.beforeEach( ( to, from, next ) => {
  if ( to.path === '/login' ) {
    if ( localStorage.token ) {
      next( '/home' );
    } else {
      next();
    }
  } else {
    if ( localStorage.token ) {
      next();
    } else {
      next( '/login' );
    }
  }
})

export default router;